package cn.first;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//告诉 servlet 用 utf-8  解码
		req.setCharacterEncoding("utf-8");	//优先级极低，必须放在第一句
		//告诉 servlet 用 utf-8 编码
		resp.setCharacterEncoding("utf-8");
		//告诉 浏览器 用 utf-8 解析
		resp.setContentType("text/plain;charset=utf-8");
		resp.getWriter().println("It is the post response ...");
		
		String user=req.getParameter("username");
		String passwd=req.getParameter("password");
		System.out.println(user);
		resp.getWriter().println(user);
		
		if(user.equals(getServletContext().getInitParameter("username")) && passwd.equals(getServletContext().getInitParameter("password"))) {		
			resp.getWriter().println("Login success!");
		}else {
			resp.getWriter().println("Login fail!");
		}		
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().println("It is the get response ...");
		
		String user=req.getParameter("username");
		String passwd=req.getParameter("password");	
		System.out.println(user);
		resp.getWriter().println(user);
		
		if(user.equals(getServletContext().getInitParameter("username")) && passwd.equals(getServletContext().getInitParameter("password"))) {
			resp.getWriter().println("Login success!");
		}else {
			resp.getWriter().println("Login fail!");
		}
	}
}
